package com.elitesland.org.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.core.base.PagingVO;
import com.elitesland.org.param.EmployeeQParam;
import com.elitesland.org.param.EmployeeSave;
import com.elitesland.org.service.EmployeeService;
import com.elitesland.org.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/org/employees")
@Api(value = "组织结构", tags = {"雇员管理"})
public class EmployeeController {

    private EmployeeService employeeService;

    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @PostMapping("/q")
    @ApiOperation("检索雇员信息")
    public ApiResult<PagingVO<EmployeeVO>> search(@RequestBody EmployeeQParam param) {
        val ret = employeeService.search(param);
        return ApiResult.ok(ret);
    }

    @PostMapping("/q/refs")
    @ApiOperation("检索BU关联雇员信息")
    public ApiResult<PagingVO<EmployeeVO>> searchRefs(@RequestBody EmployeeQParam param) {
        val ret = employeeService.searchRefs(param);
        return ApiResult.ok(ret);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据雇员记录ID返回雇员信息")
    public ApiResult<EmployeeVO> oneById(@PathVariable("id") Long id) {
        return employeeService.oneById(id).map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @PostMapping
    @ApiOperation("新建雇员信息")
    public ApiResult<Long> create(@RequestBody EmployeeSave employee) {
        val id = employeeService.create(employee);
        return ApiResult.ok(id);
    }

    @PutMapping
    @ApiOperation("更新雇员信息")
    public ApiResult<Object> update(@RequestBody EmployeeSave employee) {
        employeeService.update(employee);
        return ApiResult.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据雇员记录ID，删除对应雇员信息")
    public ApiResult<Object> deleteById(@PathVariable Long id) {
        employeeService.removeById(id);
        return ApiResult.ok();
    }

    @PutMapping("/{employeeid}/bind/{sysusername}")
    @ApiOperation("把系统账号，雇员记录ID进行相互绑定")
    public ApiResult<?> bindSysUsernameToEmployee(
            @PathVariable Long employeeid,
            @PathVariable String sysusername
    ) {
        employeeService.bindSysUserToEmployee(sysusername, employeeid);
        return ApiResult.ok();
    }

    @PutMapping("/{employeeid}/unbind")
    @ApiOperation("根据雇员记录ID，解绑系统账号")
    public ApiResult<?> unbindEmployeeWithSysUser(
            @PathVariable Long employeeid
    ) {
        employeeService.unbindEmployeeWithSysUser(employeeid);
        return ApiResult.ok();
    }

    @PutMapping("/{employeeid}/reportto/{id}")
    @ApiOperation("绑定雇员到汇报上级")
    public ApiResult<?> bindReportTo(
            @PathVariable Long employeeid,
            @PathVariable Long id
    ) {
        employeeService.bindReportTo(employeeid, id);
        return ApiResult.ok();
    }

    @PutMapping("/{employeeid}/unbindreport")
    @ApiOperation("解除汇报关系")
    public ApiResult<?> unbindReportTo(
            @PathVariable Long employeeid
    ) {
        employeeService.unbindReportTo(employeeid);
        return ApiResult.ok();
    }

    @GetMapping("/{employeeid}/reportto")
    @ApiOperation("返回上级雇员信息")
    public ApiResult<?> findReportTo(
            @PathVariable Long employeeid
    ) {
        return employeeService.findReportTo(employeeid).map(ApiResult::ok)
                .orElse(ApiResult.fail(ApiCode.NOT_FOUND));
    }

    @GetMapping("/{employeeid}/reportsubsall")
    @ApiOperation("返回所有汇报下级雇员信息")
    public ApiResult<?> listReportSubsAll(
            @PathVariable Long employeeid
    ) {
        return ApiResult.ok(
                employeeService.listReportSubsAll(employeeid)
        );
    }

    @GetMapping("/{employeeid}/reportsubs")
    @ApiOperation("返回所有直属汇报下级雇员信息")
    public ApiResult<?> listReortSubs(
            @PathVariable Long employeeid
    ) {
        return ApiResult.ok(
                employeeService.listReportSubs(employeeid)
        );
    }
}
